package com.ysqcould.allocation.service.impl;


import cn.hutool.core.util.ObjectUtil;
import com.ysqcould.allocation.domain.WxList;
import com.ysqcould.allocation.domain.WxUserContacts;
import com.ysqcould.allocation.mapper.WxUserContactsMapper;
import com.ysqcould.allocation.service.IWxUserContactsService;
import com.ysqcould.allocation.utils.UploadFileUtil;
import com.ysqcould.common.exception.ServiceException;
import com.ysqcould.common.utils.DateUtils;
import com.ysqcould.common.utils.GdMapUtils;
import com.ysqcould.common.utils.PyUtils;
import com.ysqcould.common.utils.StringUtils;
import com.ysqcould.common.utils.bean.BeanValidators;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.Validator;
import java.util.List;

/**
 * 自动认证会员数据Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-06-30
 */
@Slf4j
@Service
public class WxUserContactsServiceImpl implements IWxUserContactsService
{
    @Autowired
    private WxUserContactsMapper wxUserContactsMapper;

    @Autowired
    protected Validator validator;

    /**
     * 查询自动认证会员数据
     * 
     * @param id 自动认证会员数据主键
     * @return 自动认证会员数据
     */
    @Override
    public WxUserContacts selectWxUserContactsById(Long id)
    {
        return wxUserContactsMapper.selectWxUserContactsById(id);
    }

    /**
     * 查询自动认证会员数据列表
     * 
     * @param wxUserContacts 自动认证会员数据
     * @return 自动认证会员数据
     */
    @Override
    public List<WxUserContacts> selectWxUserContactsList(WxUserContacts wxUserContacts)
    {
        return wxUserContactsMapper.selectWxUserContactsList(wxUserContacts);
    }

    /**
     * 新增自动认证会员数据
     * 
     * @param wxUserContacts 自动认证会员数据
     * @return 结果
     */
    @Override
    public int insertWxUserContacts(WxUserContacts wxUserContacts)
    {
        wxUserContacts.setCreateTime(DateUtils.getNowDate());
        return wxUserContactsMapper.insertWxUserContacts(wxUserContacts);
    }

    /**
     * 修改自动认证会员数据
     * 
     * @param wxUserContacts 自动认证会员数据
     * @return 结果
     */
    @Override
    public int updateWxUserContacts(WxUserContacts wxUserContacts)
    {
        wxUserContacts.setUpdateTime(DateUtils.getNowDate());
        return wxUserContactsMapper.updateWxUserContacts(wxUserContacts);
    }

    /**
     * 批量删除自动认证会员数据
     * 
     * @param ids 需要删除的自动认证会员数据主键
     * @return 结果
     */
    @Override
    public int deleteWxUserContactsByIds(Long[] ids)
    {
        return wxUserContactsMapper.deleteWxUserContactsByIds(ids);
    }

    /**
     * 删除自动认证会员数据信息
     * 
     * @param id 自动认证会员数据主键
     * @return 结果
     */
    @Override
    public int deleteWxUserContactsById(Long id)
    {
        return wxUserContactsMapper.deleteWxUserContactsById(id);
    }

    @Override
    public int updateStatus(WxUserContacts contacts) {
        WxUserContacts wxUserContacts = wxUserContactsMapper.selectWxUserContactsById(contacts.getId());
        wxUserContacts.setStatus(wxUserContacts.getStatus());
        return wxUserContactsMapper.updateWxUserContacts(wxUserContacts);
    }

    @Override
    public String importUser(List<WxUserContacts> wxList, boolean updateSupport) {
        if (StringUtils.isNull(wxList) || wxList.size() == 0)
        {
            throw new ServiceException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        long time=0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (WxUserContacts list : wxList)
        {
            final String lonLat = GdMapUtils.getLonLat(list.getAddress());
            try
            {
                // 验证是否存在这个用户
                WxUserContacts u = wxUserContactsMapper.selectContact(list.getName(),list.getCompanyName(),list.getInternalJob());
                if (ObjectUtil.isNull(u))
                {
                    final long startTime = System.currentTimeMillis();
                    BeanValidators.validateWithException(validator, list);
                    final String yin = PyUtils.getFirstSpellPinYin(list.getName());
                    list.setNamePy(yin);
                    list.setAddrCoords(lonLat);
                    this.insertWxUserContacts(list);
                    final long endTime = System.currentTimeMillis();
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、姓名 " + list.getName() + " 导入成功"+"总共耗时:"+(endTime-startTime));
                }
                else if (updateSupport)
                {
                    BeanValidators.validateWithException(validator, list);
                    list.setId(u.getId());
                    list.setAddrCoords(lonLat);
                    list.setOpenid(u.getOpenid());
                    list.setNamePy(u.getNamePy());
                    this.updateWxUserContacts(list);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、姓名 " + list.getName() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、姓名 " + list.getName() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、姓名 " + list.getName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
